home *** CD-ROM | disk | FTP | other *** search
- Path: news.cityu.edu.hk!92907308
- From: 92907308@cpccux0.cityu.edu.hk ()
- Newsgroups: comp.lang.c++
- Subject: 2D pointer array to class overwritten
- Date: 7 Apr 1996 11:35:53 GMT
- Organization: City University of Hong Kong
- Message-ID: <4k89ap$7fl@ctylnk.cityu.edu.hk>
- NNTP-Posting-Host: cpccux1.cityu.edu.hk
- X-Newsreader: TIN [version 1.2 PL2]
-
-
- Hello,
-
- I have a difficulty with a 2D array of pointers to a class.
- The addresses of new objects overwrite the addresses of the objects
- at the upper region of the array. The program is :
-
- #include <fstream.h>
- class cl
- {
- int a;
- };
-
- void main(void)
- {
- cl *ptr[4,4];
- int i,j;
-
- for (i=1; i<=3; i++)
- {
- cout << "\n";
- for (j=1; j<=3; j++)
- {
- ptr[i,j]=new cl;
- cout << " "<< ptr[i,j];
- }
- }
- cout << "\n";
- for (i=1; i<=3; i++)
- {
- cout << "\n";
- for (j=1; j<=3; j++)
- cout << " "<< ptr[i,j];
- }
- }
-
-
- And it produces the following output:
-
- 0x23530ef6 0x23530efe 0x23530f06
- 0x23530f0e 0x23530f16 0x23530f1e
- 0x23530f26 0x23530f2e 0x23530f36
-
- 0x23530f26 0x23530f2e 0x23530f36
- 0x23530f26 0x23530f2e 0x23530f36
- 0x23530f26 0x23530f2e 0x23530f36
-
-
- The address assignment statement seems to assign the newly constructed
- objects to the whole [x,1] region. Why?
-
- I surely want the array of address of the first part
- of the output. How can I fix this?
-
- Thanks a lot and please reply by mail.
-
- Ken
-